<?php include_once("../../common/common_setting_unit.php");         // 引入通用的设置文件 ?>
<?php include_once(ROOT_PATH_ . "/controller/common/module/head.php"); // 引入通用的头部 ?>





<?php


//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$brand_id    = addslashes($_REQUEST['brand_id']);
$nowpageid    = addslashes($_REQUEST['nowpageid']);
$sortby       = addslashes($_REQUEST['sortby']);
$term_nid     = addslashes($_REQUEST['term_nid']);
$system_part  = addslashes($_REQUEST['now_extender']);


$cat_list_json = json_decode($_REQUEST['cat_list_json'],true);  //关联文章信息处理

$new_cat_list = '';

if($_REQUEST['cat_list_json'] != ''){
    $cat_list_arr = array();
    foreach($cat_list_json as $cat_val){

        if(!empty($cat_val['children'])){
            $cat_list_arr [] = get_deep_id($cat_val['children']);
        }
        else{
            $cat_list_arr [] = $cat_val['id'];
        }

    }

    $new_cat_list = toOneArray($cat_list_arr);

}






//从组件获取的品牌的数据
$brand_list_json = json_decode($_REQUEST['brand_list_json'],true);

//print_r2($brand_list_json);


$new_brand_list = '';





//处理二级
if($_REQUEST['brand_list_json'] != '[]'){
    $brand_list_arr = array();
    foreach($brand_list_json as $brand_val){

        //父子联动启动的时候写法，只记录子类
//		if(!empty($brand_val['children'])){
//			$brand_list_arr [] = get_deep_id($brand_val['children']);
//		}
//		else{
//			$brand_list_arr [] = $brand_val['id'];
//		}

        //父子联动取消的时候写法，记录父类和子类
        $brand_list_arr [] = get_deep_id($brand_val['children']);
        $brand_list_arr [] = $brand_val['id'];

    }

    //print_r2($brand_list_arr);
    static $res_arr = array();
    $new_brand_list = toOneArray($brand_list_arr,true);

}





$shop_list_json = json_decode($_REQUEST['shop_list_json'],true);  //关联文章信息处理


$new_shop_list = '';

if($_REQUEST['shop_list_json'] != '[]'){
    $shop_list_arr = array();
    foreach($shop_list_json as $shop_val){

        if(!empty($shop_val['children'])){
            $shop_list_arr [] = get_deep_id($shop_val['children']);
        }
        else{
            $shop_list_arr [] = $shop_val['id'];
        }

    }
    static $res_arr = array();
    $new_shop_list = toOneArray($shop_list_arr,true);

}



function toOneArray($array,$clear=false)
{
    static $res_arr = array();
    if ($clear)
    {
        $res_arr = array();
    }
    foreach ($array as $v) {
        if (is_array($v)) {
            toOneArray($v);
        } else {
            $res_arr[] = $v;
        }
    }
    return $res_arr;
}

function get_deep_id($list){
    $id_arr = array();
    foreach($list as $val){
        if(!empty($val['children'])){
            return  get_deep_id($val['children']);
        }

        $id_arr[] = $val['id'];

    }

    return $id_arr;
}





//所有插入新项的代码写在这里。
//====================================================
//设置表格和默认排序
$table_Suffix = $sys_list_table;
$table        = $table_Prefix.$table_Suffix;

$detail_attr_list = json_decode($_REQUEST['detail_attr'],true);  //关联文章信息处理

$goods_attr_list = json_decode($_REQUEST['goods_attr'],true);  //关联文章信息处理




//处理一下要更新的数据
//检查表是否有这个字段,如果没有自动生成,
//unid是数据指纹。20200223126003+xxxxxxxxxxxx10位随机数组成，是绝对唯一的

if(!$old_detail['unid'])
{
    $_REQUEST['ai_unid']         = date("YmdHis").rand(1000000000,9999999999);
}



//用程序消除外链地址最后打斜杠'/','\'

//把字符串中的反斜杠转义成斜杠
$_REQUEST['ai_storage'] = str_replace("\\", "/", $_REQUEST['ai_storage']);

/*转义之后反斜杠会变成'//',函数中在末尾添加@，变成//@,用来和http://中的区分*/
if (substr($_REQUEST['ai_storage'], -2, 2) == "//") {
    $_REQUEST['ai_storage'] = $_REQUEST['ai_storage'] . "@";
    $_REQUEST['ai_storage'] = str_replace("//@", "", $_REQUEST['ai_storage']);
}

if (substr($_REQUEST['ai_storage'], -1, 1) == "/") {
    $_REQUEST['ai_storage'] = $_REQUEST['ai_storage'] . "@";
    $_REQUEST['ai_storage'] = str_replace("/@", "", $_REQUEST['ai_storage']);
}

?>





<?
//关于时间的一些数据,修正一下数据
//如果传过来的时间是空的或者错误的（错误判别为出现1970），就用现在时间替换。

if((!$_REQUEST['ai_publish'])or(strstr($_REQUEST['ai_publish'],"1970")))
{
    $_REQUEST['ai_publish']=date("Y-m-d H:s:i");
}


if((!$_REQUEST['ai_inputtime'])or(strstr($_REQUEST['ai_inputtime'],"1970")))
{
    echo $_REQUEST['ai_inputtime']=date("Y-m-d H:s:i");
}


if((!$_REQUEST['ai_modifytime'])or(strstr($_REQUEST['ai_modifytime'],"1970")))
{
    $_REQUEST['ai_modifytime']=date("Y-m-d H:s:i");
}

$_REQUEST['ai_belong'] = 'product';
$sql = insert_data($table_prefix."detail",$_REQUEST);


mysql_query($sql) or die ('添加数据失败'. mysql_error());

$new_detail_nid =  mysql_insert_id();

?>






<?

/*插入新的detail与term关系表*/


/*思路是
1.分解传递过来的分类号码
2.查detail对term数据是否存在
3.存在就不用理，不存在就插入



那么怎么删除呢？

思路是
分解传递过来的分类号码

查出所有detail对term数据
detail能对得上新的term，就保留
detail对不上term，就删除

那么怎么删除呢
*/


//设置需要搜索的表格的前缀后缀
$table_Suffix = "relation";
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//1.checkbox传递过来的数组分解开

$term_detail_list = $new_cat_list;

if($term_detail_list ){


    $cat_list_id= array();


        foreach($term_detail_list as $value2){
            $sql = "INSERT INTO $table (
					detail_nid,
					term_nid
					)
					
					value (
					
					'$new_detail_nid',
					'$value2'
					)";

            sql_log($sql);
            $query = mysql_query($sql) or die ('添加数据失败 591 ：'. mysql_error());
        }

}

$brand_detail_list = $new_brand_list;;

$brand_list_id= array();


    if($brand_detail_list){
        foreach($brand_detail_list as $value2){
            $sql = "INSERT INTO $table (
						detail_nid,
						term_nid,
						belong
						)
						
						value (
						
						'$new_detail_nid',
						'$value2',
						'brand'
						)";

            sql_log($sql);
            $query = mysql_query($sql) or die ('672 添加数据失败'. mysql_error());
        }

}



$shop_detail_list = $new_shop_list;;


$shop_list_id= array();


    if($shop_detail_list){
        foreach($shop_detail_list as $value2){
            $sql = "INSERT INTO $table (
						detail_nid,
						term_nid,
						belong
						)
						
						value (
						
						'$new_detail_nid',
						'$value2',
						'shop'
						)";

            sql_log($sql);
            $query = mysql_query($sql) or die ('添加数据失败'. mysql_error());
        }

}


?>








<script type="text/javascript">

    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "view/data_list.php?brand_id=<? echo $brand_id ?>&nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $term_nid ?>&now_extender=<? echo $system_part ?>"

</script>



